home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Applications / Calculators / Calculette / Source / Engine.y < prev    next >
Text File  |  1993-10-29  |  1KB  |  75 lines

  1. %{
  2. #include <stdio.h>
  3.  
  4. #define YYSTYPE double
  5.  
  6. static int prec = 0;
  7. static YYSTYPE mem = 0;
  8. %}
  9.  
  10. %start list
  11.  
  12. %token NUMBER
  13.  
  14. %left '+' '-'
  15. %left '*' '/'
  16. %left '%'
  17. %left UMINUS
  18.  
  19. %%
  20.  
  21. list:    /* empty */
  22.     | list '\n'
  23.     | list error '\n'    { yyerrok; }
  24.     | list expr '\n'    { printf( "%.*f\n", prec, $2 ); fflush( stdout ); }
  25.     | list asgn '\n'
  26.     ;
  27.  
  28. expr:      '-' expr %prec UMINUS    { $$ = -$2; }
  29.     | expr '+' expr        { $$ = $1 + $3; }
  30.     | expr '-' expr        { $$ = $1 - $3; }
  31.     | expr '*' expr        { $$ = $1 * $3; }
  32.     | expr '/' expr        { $$ = $1 / $3; }
  33.     | expr '%' expr        { $$ = $1 * ($3 / 100); }
  34.     | 'R'            { $$ = mem; }
  35.     | NUMBER
  36.     ;
  37.  
  38. asgn:      'P' NUMBER        { prec = $2; }
  39.     | 'A' NUMBER        { mem += $2; }
  40.     | 'S' NUMBER        { mem -= $2; }
  41.     | 'C'            { mem = 0.0; }
  42.     ;
  43. %%
  44.  
  45. void yyerror( char *s )
  46. {
  47. }
  48.  
  49. int yylex( void )
  50. {
  51.   int c;
  52.  
  53.   while ( (c = getchar( )) == ' ' || c == '\t' )
  54.     ;    /* skip white spaces */
  55.  
  56.   if ( c == EOF )
  57.     return 0;
  58.  
  59.   if ( c == '.' || isdigit( c )) {
  60.     ungetc( c, stdin );
  61.     scanf( "%lf", &yylval );
  62.     return NUMBER;
  63.   }
  64.  
  65.   return c;
  66. }
  67.  
  68. void main( )
  69. {
  70.   while ( !feof( stdin ))
  71.     yyparse( );
  72.  
  73.   exit( 0 );
  74. }
  75.